package cums.gui.action;

import antlr.debug.Event;
import cums.logic.json.DateJsonValueProcessor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cums.db.pers.Cusevent;
import cums.db.pers.Cusgrp;
import cums.db.pers.Cusprop;
import cums.db.pers.Customers;
import cums.db.pers.Groups;
import cums.db.pers.Propdetail;
import cums.db.pers.Properties;
import cums.db.pers.Users;
import cums.logic.iface.CustomerService;
import cums.logic.iface.UserService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

/**
 * @time Dec 10, 2009, 3:28:51 AM
 * @author fdcube
 */
public class CusAct extends ActionSupport {

    public String propname, propdetail, ename, edetail, gname, gid, cid, cname, cphone, cmail, cbirthday;
    public String uname, upwd;
    private JSONArray jsonArray = new JSONArray();
    private CustomerService customerService;
    private UserService userService;
    private static final CycleDetectionStrategy CycleDetectionStrategy = null;
    private Map session = ActionContext.getContext().getSession();

    public void setGname(String gname) {
        this.gname = gname;
    }

    public void setPropdetail(String propdetail) {
        this.propdetail = propdetail;
    }

    public void setPropname(String propname) {
        this.propname = propname;
    }

    public void setEdetail(String edetail) {
        this.edetail = edetail;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setGid(String gid) {
        this.gid = gid;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public void setCbirthday(String cbirthday) {
        this.cbirthday = cbirthday;
    }

    public void setCmail(String cmail) {
        this.cmail = cmail;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public void setCphone(String cphone) {
        this.cphone = cphone;
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }

    public JSONArray getJsonArray() {
        return jsonArray;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public String addCustomer() {
        try {
            System.out.println(cname);
            Customers custmp = new Customers((new Users(Integer.parseInt(session.get("uid").toString()))), cname, cmail, cphone, cbirthday);
            custmp = customerService.AddOrUpdateCus(custmp);
            setJson(custmp, false);
            return Action.SUCCESS;
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
            return Action.SUCCESS;
        }
    }

    public String changeGroup() {
        try {
            Cusgrp gtmp = new Cusgrp(new Customers(Integer.parseInt(cid)), new Groups(Integer.parseInt(gid)));
            gtmp = customerService.AddOrUpdateCusg(gtmp);
            jsonArray.add("1");
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
        }
        return Action.SUCCESS;
    }

    public String addGroup() {
        try {
            Groups gtmp = new Groups(gname);
            gtmp = customerService.AddOrUpdateGroup(gtmp);
            setJson(gtmp, false);
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
        }
        return Action.SUCCESS;
    }

    public String addEvent() {
        try {
            Cusevent etmp = new Cusevent(new Customers(Integer.parseInt(cid)), ename, new Date(), edetail);
            etmp = customerService.AddOrUpdateCue(etmp);
            setJson(etmp, false);
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
        }
        return Action.SUCCESS;
    }

    public String addProp() {
        try {
            Properties ptmp = new Properties(propname);
            ptmp = customerService.AddOrUpdateProp(ptmp);
            Propdetail pdtmp = new Propdetail(ptmp, propdetail);
            pdtmp = customerService.AddOrUpdatePropDet(pdtmp);
            Cusprop cptmp = new Cusprop(new Customers(Integer.parseInt(cid)), pdtmp);
            cptmp = customerService.AddOrUpdateCusprop(cptmp);
            setJson(cptmp, false);
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
        }
        return Action.SUCCESS;
    }

    public String deleteCustomer() {
        Customers custmp = new Customers(Integer.parseInt(cid));
        if (customerService.DeleteCus(custmp)) {
            jsonArray.add("1");
        } else {
            jsonArray.add("0");
        }
        return Action.SUCCESS;
    }

    public String updateCustomer() {
        try {
            Customers custmp = new Customers(Integer.parseInt(cid), (new Users(Integer.parseInt(session.get("uid").toString()))), cname, cmail, cphone, cbirthday);
            custmp = customerService.AddOrUpdateCus(custmp);
            setJson(custmp, false);
            return Action.SUCCESS;
        } catch (Exception e) {
            System.out.println(e);
            jsonArray.add("0");
            return Action.SUCCESS;
        }
    }

    public String userLogout() {
        session.remove("uid");
        return Action.SUCCESS;
    }

    public String userLogin() {
        System.out.println(uname + "------" + upwd);
        Users usertmp = new Users(uname, upwd);
        System.out.println(usertmp.getUname() + usertmp.getUpwd());
        try {
            System.out.println("-----------UsersLogin not Work yet-------");
            int uidd = userService.Login(usertmp).getUid();
            System.out.println("-----------UsersLogin Work!-------");
            session.put("uid", uidd);
            this.setUname(null);
            this.setUpwd(null);
            jsonArray.add("1");
            return Action.SUCCESS;
        } catch (Exception e) {
            System.out.println("userLogin:" + e);
            this.setUname(null);
            this.setUpwd(null);
            jsonArray.add("0");
            return Action.SUCCESS;
        }
    }

    public void setJson(Object o, boolean array) {
        JsonConfig config = new JsonConfig();
        config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
        config.setArrayMode(JsonConfig.MODE_LIST);
        config.setIgnoreDefaultExcludes(false);
        //config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        config.setExcludes(
                new String[]{"hibernateLazyInitializer",
                    "handler", "customerses", "customers", "users"});
        if (array) {
            jsonArray = (JSONArray) JSONSerializer.toJSON(o, config);
        } else {
            jsonArray.add(JSONSerializer.toJSON(o, config));
        }
    }

    @Override
    public String execute() {

//        config.setIgnoreDefaultExcludes(false);

//        config.registerJsonBeanProcessor(
//                org.hibernate.proxy.HibernateProxy.class,
//                new HibernateJsonBeanProcessor());
//        config.setJsonBeanProcessorMatcher(new HibernateJsonBeanProcessorMatcher());
//        config.setIgnoreDefaultExcludes(false);

//        config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
//        JsonConfig config = new JsonConfig();
//        config.setJsonPropertyFilter(new PropertyFilter() {
//
//            public boolean apply(Object source, String name, Object value) {
//                if (name.equals("properties") || name.equals("customers") || name.equals("propdetail") || name.equals("groups")|| name.equals("cusevent")|| name.equals("users")) {
//                    return true;
//                } else {
//                    return false;
//                }
//            }
//        });
        try {
            List<Customers> CusList = customerService.ListCustomersByUid(0, Integer.parseInt(session.get("uid").toString()));
//            jsonArray = JSONArray.fromObject(CusList, config);
            setJson(CusList, true);
        } catch (Exception e) {
            jsonArray.add("login");
            System.out.println(e);
        }
        System.out.println("------" + jsonArray.toString() + "-------");
        return Action.SUCCESS;
    }
}
